<template>
  <div class="login-page">
    <el-card class="box-card">
      <div slot="header" class="clearfix">

        <span class="login-title">🔐教师验证码登录</span>
      </div>
      <div class="login-form">
        <el-form :model="form" :rules="loginRules" ref="loginForm">
          <el-form-item prop="email">
            <el-input type="text" v-model="form.email" auto-complete="off" placeholder="请输入QQ号">
              <template slot="prepend"><i style="font-size:20px" class="el-icon-user"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="verifycode">
            <div style="display: flex;">
              <el-input type="text" v-model="form.verifycode" auto-complete="off" placeholder="请输入验证码">
                <template slot="prepend"><i style="font-size:20px" class="el-icon-key"></i></template>
              </el-input>
              <el-button @click="getverifycode" style="width: 150px;" type="primary" :disabled="countDown > 0">
                <span v-if="countDown === 0">获取验证码</span>
                <span v-else>{{ countDown }} 秒后获取</span>
              </el-button>

            </div>
          </el-form-item>


          <el-form-item>
            <el-button style="width:340px;" type="primary" @click="handleLogin" :loading="loading">登录</el-button>
          </el-form-item>
          <el-form-item>
            <el-link href="/LoginByPasswordForTea" style="color: white">
              <el-button style="width:340px;" type="primary">
                账号密码登录
              </el-button>
            </el-link>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
  <el-button @click="ddd">dddd</el-button>
</template>

<script>

import axios from "axios";
import Qs from 'qs'
export default {
  name: 'Login',
  mounted() {
    localStorage.removeItem('token');
  },
  data() {
    return {
      countDown: 0,
      loading: false,
      form: {
        email: '',
        verifycode: ''
      },
      loginRules: {
        email: [
          {required: true, message: '请输入QQ号', trigger: 'blur'},
        ],
        verifycode: [
          {required: true, message: '请输入验证码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    getverifycode() {
      var emailReceiver = this.form.email;
      console.log(emailReceiver);
      axios({
        method: 'post',
        url: 'http://localhost:80/email/sendEmail',
        params: {
          emailReceiver: emailReceiver
        },
      })
          .then(res => {
            console.log(res);
            if (res.status === 200) {
              this.$message.success('验证码已发送');
            } else {
              this.$message.error('验证码发送失败');
            }
          })
          .catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.countDown = 60;
            const timer = setInterval(() => {
              if (this.countDown > 0) {
                this.countDown--;
                console.log(this.countDown)
              } else {
                clearInterval(timer);
              }
            }, 1000);
          });

    },
    handleLogin() {
      axios({
        method: 'post',
        url: 'http://localhost:80/loginbyemail1',
        params: {
          email: this.form.email,
          verifycode: this.form.verifycode
        },


      })
          .then(res => {
            console.log(res);
            console.log(res.status);
            var result = res.data
            if (result.code === 1) {
              this.$message.error('验证码错误');
              // this.$router.push('/home');
            } else {
              localStorage.setItem('token', res.data.data);
              this.$message.success('登录成功');
              this.$router.push('/Teacher_Info');
            }
          })
          .catch(err => {
            console.log(err);
          })
    },
    ddd() {
      axios({
        method: 'post',
        url: 'http://localhost:80/getinfo'

      })
          .then(res => {
            console.log(res);
            console.log(res.status);
            var result = res.data
            if (result === 2) {
              this.$message.error('验证码错误');
              // this.$router.push('/home');
            } else if (result === 1) {
              this.$message.error('QQ号错误');
            } else {

              this.$message.success('登录成功');
            }
          })
          .catch(err => {
            console.log(err);
          })
    }
  }
}
</script>

<style scoped>
.login-page{
  background-image: linear-gradient(180deg, #2af598 0%, #009efd 100%);
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-title{
  font-size: 20px;
}

.box-card {
  width: 375px;
}

</style>

